<?php

namespace Biz\model\salesRebate;

use Doraemon\pockets\datebase\BaseDbModel;

class SalesAccountLog extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_sales_rebate';

    const _DATABASE_ = 'biz_sales_rebate';

    const _TABLE_NAME = 'sales_account_log';

    const TYPE_PERSONAL = 1;//个人账户
    const TYPE_COMPANY = 2;//公司账户

    //操作类型
    const OPERATE_TYPE_SYS_ACCOUNTED = 1;//系统确认收货15天后入账
    const OPERATE_TYPE_WITHDRAWAL_APPLY = 2;//用户申请提现
    const OPERATE_TYPE_WITHDRAWAL_INVALID = 3;//用户申请提现失效4
    const OPERATE_TYPE_WITHDRAWAL_SUCCESS = 4;//财务确认转账，提现成功

    //操作人类型
    const OPERATE_USER_TYPE_SYS = 0;//系统
    const OPERATE_USER_TYPE_ADMIN = 1;//admin
    const OPERATE_USER_TYPE_USER = 2;//用户

    public static function addData($data)
    {
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('数据库操作错误');
        }
        return true;
    }

    public static function hasAccounted($uid, $type, $withdrawalCode, $receiptNumber)
    {
        $where = [
            'uid' => $uid,
            'type' => $type,
            'withdrawal_code' => $withdrawalCode,
            'operate_type' => self::OPERATE_TYPE_WITHDRAWAL_SUCCESS
        ];
        $result = self::get(['uid', 'type', 'withdrawal_code', 'receipt_number'], $where);
        if ($result) {
            if ($result['receipt_number'] == $receiptNumber) {
                throw new \Exception('已经存在的银行回单编号');
            }
            return true;
        }
        return false;
    }

    public static function hasOperated($uid, $type, $withdrawalCode, $operateType)
    {
        $where = [
            'uid' => $uid,
            'type' => $type,
            'withdrawal_code' => $withdrawalCode,
            'operate_type' => $operateType
        ];
        $result = self::has($where);
        return $result;
    }

}